package com.huilian.tool.work.weixin.dto;

import com.huilian.tool.work.weixin.enums.MediaTypeEnum;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;


/**
 * @Author 少凡
 * @Date 2022-07-09 16:53
 * version 1.0
 */
@Data
public class Moment {

    //	文本消息
    private Media.Text text = new Media.Text();

    //附件，不能与text.content同时为空，最多支持9个图片类型，或者1个视频，或者1个链接。类型只能三选一，若传了不同类型，报错'invalid attachments msgtype'

    private List<Attachment> attachments = new ArrayList<>();

    //指定的发表范围；若未指定，则表示执行者为应用可见范围内所有成员
    private VisibleRange visible_range = new VisibleRange();

    public void setAttachments(List<Attachment> attachments) {
        attachments.forEach( attachment -> {
            if (!attachment.getMsgtype().equals(attachments.get(0).getMsgtype())){
                throw new RuntimeException("附件必须为同一类型");
            }

            switch (attachment.getMsgtype()){
                case "image":
                    if (attachments.size() >9) {
                        throw new RuntimeException("图片消息附件，数量超过最大支持数量9张");
                    }
                    break;
                case "video":
                    if (attachments.size() >1) {
                        throw new RuntimeException("视频消息附件，只支持上传1个");
                    }
                    break;
                case "link":
                    if (attachments.size() >1) {
                        throw new RuntimeException("链接类型附件，只支持上传1个");
                    }
                    break;
            }
        });
        this.attachments = attachments;
    }


    @Data
    public class VisibleRange {

        private SenderList sender_list = new SenderList();
        private ExternalContactList external_contact_list = new ExternalContactList();


        @Data
        public class SenderList {
            private String[] user_list;
            private String[] department_list;
        }

        @Data
        public class ExternalContactList {
            private String[] tag_list;
        }

    }
}
